//index.js
//获取应用实例
var app = getApp();

Page({
    data: {
        siteInfo: app.siteInfo,
        navBtns: [],
        showLoading: 1,
        navCurrentCid: -1,
        liveList: [],
        pid: 1,
        page_num: 20,
    },

    onLoad: function (event) {
        var that = this;
        app.initPage();
        app.globalData.showLoading = this.data.showLoading;

        app.util.request({
            'url': 'index/siteInfo',
            'cachetime': '600',
            success: function (res) {
                var data = res.data.data;
                that.setData({
                    globalSiteInfo: data
                })
            }
        });

        app.util.request({
            'url': 'index/appCheckData',
            'cachetime': '30',
            'showLoading': false,
            success: function (res) {
                var data = res.data.data;
                that.setData({
                    'checkData': res.data.data,
                });
                if (data.status == 1) {
                    wx.setNavigationBarTitle({title: data.title});
                    that.setData({
                        'showLoading': 0,
                    });
                    app.globalData.showLoading = 0;
                } else {
                    app.util.footer(that);
                    //初始化头分类
                    app.util.request({
                        'url': 'index/index_category',
                        'cachetime': '300',
                        'showLoading': false,
                        success: function (res) {
                            that.setData({
                                'navBtns': res.data.data,
                            });
                        }
                    });

                    //初始化列表
                    var param = {};
                    param.cid = that.data.navCurrentCid;
                    app.util.request({
                        'url': 'index/index_live_list',
                        'showLoading': false,
                        data: param,
                        success: function (res) {
                            that.setData({
                                'liveList': res.data.data,
                                'showLoading': 0,
                            });
                        }
                    });
                    app.globalData.showLoading = 0;
                }
            }
        });
        that.initShatData();
    },
    onShow: function (e) {
    },
    //跳转到门店地址
    toaddress: function () {
        var that = this;
        wx.openLocation({
            'latitude': parseFloat(that.data.checkData.latitude),
            'longitude': parseFloat(that.data.checkData.longitude),
        })
    },
    checkCell: function () {
        var that = this;
        wx.makePhoneCall({
            phoneNumber: that.data.checkData.tel //仅为示例，并非真实的电话号码
        })
    },
    //下拉刷新
    onPullDownRefresh: function () {
        var that = this;
        if (that.data.checkData.status == 1) {
            return;
        }
        //初始化列表
        var param = {};
        param.pid = 1;
        param.cid = that.data.navCurrentCid;
        app.util.request({
            'url': 'index/index_live_list',
            data: param,
            success: function (res) {
                that.setData({
                    'liveList': res.data.data,
                });
                wx.stopPullDownRefresh();
            }
        });
    },
    //上拉加载
    onReachBottom: function () {
        var that = this;
        if (that.data.checkData.status == 1) {
            return;
        }
        var liveList = this.data.liveList;
        var pid = this.data.pid;
        pid++;
        this.setData({
            pid: pid,
        });

        var param = {};
        param.pid = pid;
        param.cid = that.data.navCurrentCid;
        app.util.request({
            'url': 'index/index_live_list',
            data: param,
            success: function (res) {
                for (var i = 0; i < res.data.data.length; i++) {
                    liveList.push(res.data.data[i]);
                }
                that.setData({
                    'liveList': liveList,
                });
            }
        });
    },

    //点击头部导航
    changeNavBtn: function (event) {
        var that = this;
        var cid = event.currentTarget.dataset.cid;
        if (cid == -3) {
            app.util.getUserInfo(function (userInfo) {
            })
        }
        this.setData({
            navCurrentCid: cid,
            pid: 1,
        });
        var param = {};
        param.pid = 1;
        param.cid = cid;
        app.util.request({
            'url': 'index/index_live_list',
            data: param,
            success: function (res) {
                that.setData({
                    'liveList': res.data.data,
                });
            }
        });
    },

    // 跳转直播详情
    linkLiveDetail: function (event) {
        var id = event.currentTarget.dataset.item.id;
        wx.navigateTo({
            url: '/wxz_superstore/pages/zbDetail/zbDetail?id=' + id
        })
    },
    initShatData: function () {
        var that = this;
        app.util.request({
            'url': 'setting/get_share',
            'cachetime': '30',
            'data': {'route': app.util.getCurrentPageUrlWithArgs()},
            'showLoading': false,
            success: function (res) {
                var data = res.data.data;
                that.setData({
                    'shareData': data,
                });
            }
        });
    },
    onShareAppMessage: function (options) {
        //ajax获取分享数据
        var that = this;
        return {
            title: that.data.shareData.title,
            imageUrl: that.data.shareData.imageUrl,
            path: that.data.shareData.path,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },
    formSubmit: function (e) {
        app.util.getUserInfo(function (userInfo) {
            var params = {};
            params.formid = e.detail.formId;
            app.util.request({
                'url': 'user/collectFormId',
                'data': params,
                success: function (res) {

                }
            })
        })
    },
    /**
     * 底部导航跳转
     */
    footerRedirect: function (e) {
        var type = e.target.dataset.type;

        if (type == 2) {
            var jumpData = e.target.dataset.jumpdata;
            //跳转到小程序
            wx.navigateToMiniProgram({
                appId: jumpData.appid,
                path: jumpData.url,
                extraData: jumpData.extra_data,
                envVersion: jumpData.env_version,
                success(res) {
                    // 打开成功
                },
                fail(res) {

                }
            })
            return;
        } else {
            wx.redirectTo({
                url: e.target.dataset.pagepath,
            });
        }
    }
})
